import subprocess
from pydub import AudioSegment
import os

def split_wav(input_file, num_parts=3):
    audio = AudioSegment.from_wav(input_file)
    part_length = len(audio) // num_parts
    output_files = []
    for i in range(num_parts):
        start_time = i * part_length
        end_time = start_time + part_length if i < num_parts - 1 else len(audio)
        part = audio[start_time:end_time]
        output_file = f"part_{i + 1}.wav"
        part.export(output_file, format="wav")
        output_files.append(output_file)
    return output_files

def wav_to_mp4_ffmpeg(wav_files, image_path="placeholder.jpg"):
    for wav_file in wav_files:
        mp4_file = os.path.splitext(wav_file)[0] + ".mp4"
        command = [
            'ffmpeg',
            '-loop', '1',
            '-i', image_path,
            '-i', wav_file,
            '-c:v', 'libx264',
            '-tune', 'stillimage',
            '-c:a', 'aac',
            '-b:a', '192k',
            '-pix_fmt', 'yuv420p',
            '-shortest',
            mp4_file
        ]
        subprocess.run(command)

if __name__ == "__main__":
    input_wav_file = "chapter04.wav"
    split_files = split_wav(input_wav_file)
    placeholder_image = "image.png"
    wav_to_mp4_ffmpeg(split_files, placeholder_image)
